package com.lihao.jetpackcore.ext.lifecycle

import android.os.Handler
import androidx.lifecycle.*

/**
 * Created by lihao on 2020/5/14.
 * Describe:
 */
class KtxHandler(lifecycleOwner: LifecycleOwner, callback: Handler.Callback) : Handler(callback),
    LifecycleObserver {

    private val mLifecycleOwner: LifecycleOwner = lifecycleOwner

    init {
        lifecycleOwner.lifecycle.addObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDestroy(){
        removeCallbacksAndMessages(null)
        mLifecycleOwner.lifecycle.removeObserver(this)
    }
}